package com.xsomnus.java_basic.juc.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author 叶小夏
 * @since 2019-08-27 15:58
 */
public class ReentrantLockTest {

    private Lock lock = new ReentrantLock();
    public Condition condition = lock.newCondition();


    public void await() {
        try {
            lock.lock();
            System.out.println("await()时间为：" + System.currentTimeMillis());
            condition.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


    public void signal() {
        try {
            lock.lock();
            System.out.println("signal()时间为：" + System.currentTimeMillis());
            condition.signal();
        } finally {
            lock.unlock();
        }
    }


    static class ThreadA extends Thread {

        private ReentrantLockTest test;

        public ThreadA(ReentrantLockTest test) {
            super();
            this.test = test;
        }

        @Override
        public void run() {
            test.await();
        }
    }

    public static void main(String[] args) throws InterruptedException {

        ReentrantLockTest test = new ReentrantLockTest();
        ThreadA a = new ThreadA(test);
        a.start();
        Thread.sleep(3000);
        test.signal();

    }

}
